package hos.filex.delegate;

import android.net.Uri;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import hos.delegate.BaseDelegate;
import hos.file.provider.FileProviderX;
import hos.file.utils.ZipImageUtils;

/**
 * <p>Title: ImageDelegate </p>
 * <p>Description: 照相代理 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-14 15:03
 */
public class CameraImageDelegate extends BaseDelegate {
    /**
     * 压缩率 ，默认100
     */
    private int leastCompressSize = 100;
    /**
     * 图片的最小压缩大小 默认 1M
     */
    private long minCompressSize = 1048576;
    @Nullable
    protected FileResult.OnResultListener mOnResultListener;
    @Nullable
    protected FileResult.OnResultListListener mOnResultListListener;

    @Nullable
    protected ZipImageUtils.OnImageZipListener mOnImageZipListener;

    @NonNull
    public static CameraImageDelegate create(ComponentActivity activity) {
        return new CameraImageDelegate(activity);
    }

    @NonNull
    public static CameraImageDelegate create(Fragment fragment) {
        return new CameraImageDelegate(fragment);
    }

    protected CameraImageDelegate(@NonNull LifecycleOwner lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        createCameraImage();
    }

    @Nullable
    private ActivityResultLauncher<String> createCameraImageLauncher;

    @NonNull
    private ActivityResultLauncher<String> createCameraImage() {
        if (createCameraImageLauncher != null) {
            return createCameraImageLauncher;
        }
        return createCameraImageLauncher = FileResultContracts.createCameraImage(requireActivityResultCaller(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri uri) {
                handleImageUri(uri);
            }
        });
    }

    //<editor-fold desc="ImageUri">

    /**
     * 处理Uri,压缩附件
     */
    protected void handleImageUri(Uri uri) {
        if (uri == null) {
            if (mOnResultListener != null) {
                mOnResultListener.onResult(null);
            }
            return;
        }
        new Thread(() -> {
            File zipImageFile = zipImageFile(uri);
            runMain(() -> {
                if (mOnResultListener != null) {
                    if (zipImageFile == null) {
                        mOnResultListener.onResult(null);
                        return;
                    }
                    mOnResultListener.onResult(zipImageFile.getAbsolutePath());
                }
            });
        }).start();
    }

    /**
     * 处理Uri,压缩附件
     */
    protected void handleImageUriList(List<Uri> uriList) {
        if (uriList == null || uriList.isEmpty()) {
            if (mOnResultListListener != null) {
                mOnResultListListener.onResult(null);
            }
            return;
        }
        new Thread(() -> {
            List<String> pathList = new ArrayList<>();
            for (Uri uri : uriList) {
                File zipImageFile = zipImageFile(uri);
                if (zipImageFile != null) {
                    pathList.add(zipImageFile.getAbsolutePath());
                }
            }
            runMain(() -> {
                if (mOnResultListListener != null) {
                    if (pathList.isEmpty()) {
                        mOnResultListListener.onResult(null);
                        return;
                    }
                    mOnResultListListener.onResult(pathList);
                }
            });
        }).start();
    }
    //</editor-fold>

    //<editor-fold desc="Zip">

    /**
     * 对图片进行压缩
     *
     * @param uri 需要压缩的Uri
     */
    @Nullable
    protected File zipImageFile(@NonNull Uri uri) {
        File file = FileProviderX.uri2File(requireActivity(), uri);
        if (file == null) {
            // 附件获取失败
            return null;
        }
        return zipImageFile(file);
    }

    /**
     * 对图片进行压缩
     *
     * @param file 需要压缩的file
     */
    @Nullable
    protected File zipImageFile(@NonNull File file) {
        return ZipImageUtils.zipImageFile(requireActivity(), file,
                minCompressSize, leastCompressSize, mOnImageZipListener);
    }
    //</editor-fold>

    //<editor-fold desc="Param">

    /**
     * 是否进行压缩
     *
     * @param isCompress true打开压缩
     */
    public CameraImageDelegate setCompress(boolean isCompress) {
        if (isCompress) {
            // 默认压缩大小 1M
            minCompressSize = 1048576;
            return this;
        }
        minCompressSize = 0;
        return this;
    }

    /**
     * 设置最小可以使用压缩
     *
     * @param minCompressSize 文件大小小于 minCompressSize 需要使用压缩
     */
    public CameraImageDelegate setMinCompressSize(long minCompressSize) {
        this.minCompressSize = minCompressSize;
        return this;
    }

    /**
     * 设置文件的压缩率
     *
     * @param leastCompressSize 文件的压缩率 默认 100
     */
    public CameraImageDelegate setLeastCompressSize(int leastCompressSize) {
        if (leastCompressSize < 60) {
            /// 最小60
            return this;
        }
        this.leastCompressSize = leastCompressSize;
        return this;
    }

    /**
     * 自定义图片压缩的方法
     *
     * @param listener 图片压缩监听
     */
    public void setOnImageZipListener(@Nullable ZipImageUtils.OnImageZipListener listener) {
        this.mOnImageZipListener = listener;
    }
    //</editor-fold>

    //<editor-fold desc="CameraImage">
    public void createCameraImage(FileResult.OnResultListener listener) {
        createCameraImage(null, listener);
    }

    public void createCameraImage(String imageName, FileResult.OnResultListener listener) {
        mOnResultListener = listener;
        try {
            createCameraImage().launch(imageName);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(null);
            }
        }
    }
    //</editor-fold>


    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        mOnResultListener = null;
        mOnResultListListener = null;
        createCameraImageLauncher = null;
        super.onDestroy(owner);
    }

}
